package com.hhxy.map;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Set;

public class MapTest1 {
    public static void main(String[] args) {
        //1.让学生选中景点，程序思维：随机生成80个A、B、C、D
        char[] chars = {'A','B','C','D'};
        Random r = new Random();
        StringBuffer bf = new StringBuffer();
        for (int i = 0; i < 80 ; i++) {
            bf.append(chars[r.nextInt(chars.length)]);
        }
        System.out.println(bf);
        //2.统计景点出现的次数，并用Map集合装起来
        Map<Character,Integer> maps = new HashMap<>();
        for (int i = 0; i <80 ; i++) {
            char ch = bf.charAt(i);
            if(maps.containsKey(ch)){
                maps.put(ch,maps.get(ch)+1);
            }else{
                maps.put(ch,1);
            }
        }
        //遍历方式1：键找值
        Set<Character> keys = maps.keySet();
        for (Character key : keys) {
            int value = maps.get(key);
            System.out.println(key+"="+value);
        }
        System.out.println("-------------------------------------");
        //遍历方式2：键值对
        Set<Map.Entry<Character, Integer>> entries = maps.entrySet();
        for (Map.Entry<Character, Integer> entry : entries) {
            char key = entry.getKey();
            int value = entry.getValue();
            System.out.println(key+"="+value);
        }
        System.out.println("-------------------------------------");
        //遍历方式3：Lambda表达式
        maps.forEach((key,value)-> System.out.println(key+"="+value));
    }
}
